package com.cwl.common.sn;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.format.FastDateFormat;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;

import javax.annotation.Resource;
import java.util.Date;

public class SnService {

    @Resource(name = "tradeSeq")
    private RedisAtomicLong transSeq;

    @Resource(name = "recordSeq")
    private RedisAtomicLong recordSeq;

    @Resource(name = "refundSeq")
    private RedisAtomicLong refundSeq;

    @Resource(name = "checkRecordSeq")
    private RedisAtomicLong checkRecordSeq;

    @Resource(name = "commonSeq")
    private RedisAtomicLong commonSeq;

    @Resource(name = "preOrderSeq")
    private RedisAtomicLong preOrderSeq;

    @Resource(name = "chargeItemSeq")
    private RedisAtomicLong chargeItemSeq;

    @Resource(name = "chargeSubjectSeq")
    private RedisAtomicLong chargeSubjectSeq;

    @Resource(name = "appSeq")
    private RedisAtomicLong appSeq;

    @Resource(name = "ScreenSeq")
    private RedisAtomicLong ScreenSeq;

    @Resource(name = "SignSeq")
    private RedisAtomicLong SignSeq;

    @Resource(name = "CallingSeq")
    private RedisAtomicLong CallingSeq;

    @Resource(name = "QueueSeq")
    private RedisAtomicLong QueueSeq;

    public String getTradeSeq(String mchNo) {
        return getTradeSeq(mchNo,"UE");
    }

    /**
     *
     * @param mchNo 商户号
     * @param cj    两位的场景值，默认为 UE
     * @return
     */
    public String getTradeSeq(String mchNo, String cj) {
        if (StringUtils.isEmpty(cj) || cj.length() != 2){
            cj = "UE";
        }
        return String.format("%s%s%06d%s",mchNo, DatePattern.PURE_DATETIME_FORMAT.format(new Date()), transSeq.getAndAdd(1) % 1000000,cj);
    }

    /**
     * 建行序列号生成
     * @param ccbMchNo
     * @return
     */
    public String getCcbSeq(String ccbMchNo) {
        return  getCcbSeq(ccbMchNo, "UE");
    }

    /**
     *
     * @param ccbMchNo  商户号
     * @param cj        两位的场景值，默认为 UE
     * @return
     */
    public String getCcbSeq(String ccbMchNo, String cj) {
        if (StringUtils.isEmpty(cj) || cj.length() != 2){
            cj = "UE";
        }
        return String.format("%s%s%03d%s",ccbMchNo, FastDateFormat.getInstance("yyMMddHHmmss").format(new Date()), transSeq.getAndAdd(1) % 1000, cj);
    }

    /**
     * 银联商务序列号生成
     * @param msgSrcId
     * @return
     */
    public String getUmsSeq(String msgSrcId) {
        return getUmsSeq(msgSrcId, "UE");
    }

    /**
     * 银联商务序列号生成
     * @param msgSrcId
     * @param cj        两位的场景值，默认为 UE
     * @return
     */
    public String getUmsSeq(String msgSrcId, String cj) {
        if (StringUtils.isEmpty(cj) || cj.length() != 2){
            cj = "UE";
        }
        return String.format("%s%s%03d",msgSrcId, FastDateFormat.getInstance("yyMMddHHmmss").format(new Date()), transSeq.getAndAdd(1) % 1000, cj);
    }

    /**
     * 获得退款单
     * @return
     */
    public String getRefundSeq(){
        return getRefundSeq("UE");
    }

    /**
     * 获得退款单
     * @param cj 两位的场景值，默认为 UE
     * @return
     */
    public String getRefundSeq(String cj){
        if (StringUtils.isEmpty(cj) || cj.length() != 2){
            cj = "UE";
        }
        return String.format("%s%s%06d%s","RF", FastDateFormat.getInstance("yyMMddHHmmss").format(new Date()), refundSeq.getAndAdd(1) % 1000000, cj);
    }


    /**
     * 银联商务退款序列号生成
     * @param msgSrcId
     * @return
     */
    public String getUmsRefundSeq(String msgSrcId) {
        return getUmsRefundSeq(msgSrcId,"UE");
    }

    /**
     * 银联商务退款序列号生成
     * @param msgSrcId
     * @param cj 两位的场景值，默认为 UE
     * @return
     */
    public String getUmsRefundSeq(String msgSrcId, String cj) {
        if (StringUtils.isEmpty(cj) || cj.length() != 2){
            cj = "UE";
        }
        return String.format("%s%s%03d%s",msgSrcId, FastDateFormat.getInstance("yyMMddHHmmss").format(new Date()), refundSeq.getAndAdd(1) % 1000,cj);
    }

    /**
     * 通用
     * @return
     */
    public String getCommonSeq(){
        return String.format("%s%s%06d", "CS", DatePattern.PURE_DATETIME_FORMAT.format(new Date()), commonSeq.getAndAdd(1) % 1000000);
    }


//    /**
//     * 预申请订单
//     * @return
//     */
//    public String getPreOrderSeq(){
//        return String.format("%s%s%06d", "PRE", DatePattern.PURE_DATETIME_FORMAT.format(new Date()), preOrderSeq.getAndAdd(1) % 1000000);
//    }
//
//    public String getItemCode(String mchNo){
//        return String.format("%s%s%s%04d", "KM",mchNo,DatePattern.PURE_DATETIME_FORMAT.format(new Date()), chargeItemSeq.getAndAdd(1)%10000);
//    }
//
//    public String getSubjectCode(String mchNo){
//        return String.format("%s%s%s%04d", "ZT",mchNo,DatePattern.PURE_DATETIME_FORMAT.format(new Date()), chargeSubjectSeq.getAndAdd(1)%10000);
//    }
//
//    public String getAppCode(String mchNo){
//
//        return String.format("%s%s%s%04d", "APP",mchNo,DatePattern.PURE_DATETIME_FORMAT.format(new Date()), appSeq.getAndAdd(1)%10000);
//    }

    public String getClinicCode(String mchNo) {
        return String.format("%s%s%03d",mchNo, DatePattern.PURE_DATETIME_FORMAT.format(new Date()), transSeq.getAndAdd(1) % 1000000);
    }

    public String getScreenNo() {
        return String.format("%s%03d","PM",ScreenSeq.getAndAdd(1) % 1000000);
    }
    public String getSignNo() {
        return String.format("%s%03d","QD",SignSeq.getAndAdd(1) % 1000000);
    }
    public String getCallingNo() {
        return String.format("%s%03d","JH",CallingSeq.getAndAdd(1) % 1000000);
    }
    public String getQueueCode() {
        return String.format("%s%06d","DL",QueueSeq.getAndAdd(1) % 1000000);
    }


}
